package com.preskool.controller;

import com.preskool.dto.ActionResult;
import com.preskool.dto.TeacherVo;
import com.preskool.po.Departments;
import com.preskool.po.Subjects;
import com.preskool.po.Teacher;
import com.preskool.po.TeacherInfo;
import com.preskool.service.DepartmentsService;
import com.preskool.service.SubjectsService;
import com.preskool.service.TeacherService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author kennyzzz
 * @Data 2021/1/7 14:25
 */
@RestController
@Slf4j
@Api(value = "Teacher接口文档")
public class TeacherController {
    @Autowired(required =false)
    private TeacherService teacherService;
    @Autowired(required = false)
    private SubjectsService subjectsService;
    @Autowired(required = false)
    private DepartmentsService departmentsService;
    @GetMapping("/teacherList")
    public ActionResult showCurrentPageTeacher(Integer currentPage,Integer pageSize,String sortName){

        List<TeacherVo> teachers = teacherService.findTeachersByPage(currentPage,pageSize,sortName);
        return new ActionResult(200,"查询成功",teachers);
    }

    @PostMapping("/teacherInfo")
    public ActionResult showTeacherInfo(@RequestParam Integer tid){
        Teacher teacher = teacherService.findTeacherByTid(tid);
        return  new ActionResult(200,"查询成功",teacher);
    }

    @PutMapping("/addTeacher")
    public ActionResult addTeacher(@RequestBody TeacherInfo teacherInfo){
        try{
            Teacher teacher = new Teacher();
            teacher.setTid(teacherInfo.getTid());
            teacher.setFulName(teacherInfo.getFulName());
            teacher.setGender(teacherInfo.getGender());
            teacher.setDob(teacherInfo.getDob());
            teacher.setMobile(teacherInfo.getMobile());
            teacher.setUsername(teacherInfo.getUsername());
            teacher.setEmail(teacherInfo.getEmail());
            teacher.setPassword(teacherInfo.getPassword());
            teacher.setAddress(teacherInfo.getAddress());
            teacher.setCity(teacherInfo.getCity());
            teacher.setState(teacherInfo.getState());
            teacher.setZipCode(teacherInfo.getZipCode());
            teacher.setCountry(teacherInfo.getCountry());
            teacher.setDeptName(teacherInfo.getDeptName());
            Subjects subjects = subjectsService.findSubjectByName(teacherInfo.getSubjectName());
            teacher.setSubid(subjects.getSubid());
            int count = teacherService.addTeacher(teacher);
            if(count>0){
                return new ActionResult(200,"添加成功",null);
            }
        }catch (Exception e){
            return new ActionResult(201,"添加失败",null);
        }

        return new ActionResult(201,"添加失败",null);
    }

    @PostMapping("/subjectList")
    public ActionResult showSubjects(){
        List<Subjects> subjects = subjectsService.findAllSubject();
        return new ActionResult(200,"success",subjects);
    }
    @PostMapping("/deptList")
    public ActionResult showdepartments(){
        List<Departments> departments = departmentsService.findAllDept();
        return new ActionResult(200,"success",departments);
    }

    @DeleteMapping("/deleteTeacher")
    public ActionResult deleteTeacher(Integer tid){
        try{
            int count = teacherService.deleteTeacherByTid(tid);
            if(count>0){
                return  new ActionResult(200,"delete success",null);
            }
        }catch (Exception e){
            return new ActionResult(204,"the teacher has a class",null);

        }
        return  new ActionResult(203,"no data",null);
    }
    @RequestMapping(value = "uptadeTeacher" , method = RequestMethod.POST)
    public ActionResult updateTeacher(@RequestBody TeacherInfo teacherInfo){
        try{
            Teacher teacher = new Teacher();
            teacher.setTid(teacherInfo.getTid());
            teacher.setFulName(teacherInfo.getFulName());
            teacher.setGender(teacherInfo.getGender());
            teacher.setDob(teacherInfo.getDob());
            teacher.setMobile(teacherInfo.getMobile());
            teacher.setUsername(teacherInfo.getUsername());
            teacher.setEmail(teacherInfo.getEmail());
            teacher.setPassword(teacherInfo.getPassword());
            teacher.setAddress(teacherInfo.getAddress());
            teacher.setCity(teacherInfo.getCity());
            teacher.setState(teacherInfo.getState());
            teacher.setZipCode(teacherInfo.getZipCode());
            teacher.setCountry(teacherInfo.getCountry());
            teacher.setDeptName(teacherInfo.getDeptName());
            Subjects subjects = subjectsService.findSubjectByName(teacherInfo.getSubjectName());
            teacher.setSubid(subjects.getSubid());
            int count = teacherService.updateTeacher(teacher);
            if(count>0){
                return new ActionResult(200,"修改成功",null);
            }
        }catch (Exception e){
            return new ActionResult(201,"修改失败",null);

        }

        return new ActionResult(201,"修改失败",null);
    }
}
